﻿using Catering_DBOpertion;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Catering_Operation
{
    /// <summary>
    /// 数据访问类:DailyMoon
    /// </summary>
    public partial class DailyMoon
    {
        public DailyMoon()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DailyMoon");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Catering_Model.DailyMoon model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DailyMoon(");
            strSql.Append("DailyManagementID,GroupID,ClassID,MoonID,DailyID,AddDate,WoJu,CanJu,Rice,Surfice,Oil,DishDate,CheckDate,IsCommunicate,Area,Oilstove,SmokeMachineWash,FireExtinguisher,FireBlanket,HighRiskEqui,EquiOperation,inWorkNum,unWorkNum,bedsnum)");
            strSql.Append(" values (");
            strSql.Append("@DailyManagementID,@GroupID,@ClassID,@MoonID,@DailyID,@AddDate,@WoJu,@CanJu,@Rice,@Surfice,@Oil,@DishDate,@CheckDate,@IsCommunicate,@Area,@Oilstove,@SmokeMachineWash,@FireExtinguisher,@FireBlanket,@HighRiskEqui,@EquiOperation,@inWorkNum,@unWorkNum,@bedsnum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@DailyManagementID", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.VarChar,50),
					new SqlParameter("@MoonID", SqlDbType.Int,4),
					new SqlParameter("@DailyID", SqlDbType.NVarChar,-1),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@WoJu", SqlDbType.Int,4),
					new SqlParameter("@CanJu", SqlDbType.Int,4),
					new SqlParameter("@Rice", SqlDbType.Decimal,9),
					new SqlParameter("@Surfice", SqlDbType.Decimal,9),
					new SqlParameter("@Oil", SqlDbType.Decimal,9),
					new SqlParameter("@DishDate", SqlDbType.DateTime),
					new SqlParameter("@CheckDate", SqlDbType.DateTime),
					new SqlParameter("@IsCommunicate", SqlDbType.Bit,1),
					new SqlParameter("@Area", SqlDbType.Decimal,9),
					new SqlParameter("@Oilstove", SqlDbType.Decimal,9),
					new SqlParameter("@SmokeMachineWash", SqlDbType.Int,4),
					new SqlParameter("@FireExtinguisher", SqlDbType.Int,4),
					new SqlParameter("@FireBlanket", SqlDbType.Int,4),
					new SqlParameter("@HighRiskEqui", SqlDbType.Int,4),
					new SqlParameter("@EquiOperation", SqlDbType.NVarChar,-1),
					new SqlParameter("@inWorkNum", SqlDbType.Int,4),
					new SqlParameter("@unWorkNum", SqlDbType.Int,4),
                    new SqlParameter("@bedsnum", SqlDbType.Int,4)};
            parameters[0].Value = model.DailyManagementID;
            parameters[1].Value = model.GroupID;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.MoonID;
            parameters[4].Value = model.DailyID;
            parameters[5].Value = model.AddDate;
            parameters[6].Value = model.WoJu;
            parameters[7].Value = model.CanJu;
            parameters[8].Value = model.Rice;
            parameters[9].Value = model.Surfice;
            parameters[10].Value = model.Oil;
            parameters[11].Value = model.DishDate;
            parameters[12].Value = model.CheckDate;
            parameters[13].Value = model.IsCommunicate;
            parameters[14].Value = model.Area;
            parameters[15].Value = model.Oilstove;
            parameters[16].Value = model.SmokeMachineWash;
            parameters[17].Value = model.FireExtinguisher;
            parameters[18].Value = model.FireBlanket;
            parameters[19].Value = model.HighRiskEqui;
            parameters[20].Value = model.EquiOperation;
            parameters[21].Value = model.inWorkNum;
            parameters[22].Value = model.unWorkNum;
            parameters[23].Value = model.bedsnum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.DailyMoon model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DailyMoon set ");
            strSql.Append("DailyManagementID=@DailyManagementID,");
            strSql.Append("GroupID=@GroupID,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("MoonID=@MoonID,");
            strSql.Append("DailyID=@DailyID,");
            strSql.Append("AddDate=@AddDate,");
            strSql.Append("WoJu=@WoJu,");
            strSql.Append("CanJu=@CanJu,");
            strSql.Append("Rice=@Rice,");
            strSql.Append("Surfice=@Surfice,");
            strSql.Append("Oil=@Oil,");
            strSql.Append("DishDate=@DishDate,");
            strSql.Append("CheckDate=@CheckDate,");
            strSql.Append("IsCommunicate=@IsCommunicate,");
            strSql.Append("Area=@Area,");
            strSql.Append("Oilstove=@Oilstove,");
            strSql.Append("SmokeMachineWash=@SmokeMachineWash,");
            strSql.Append("FireExtinguisher=@FireExtinguisher,");
            strSql.Append("FireBlanket=@FireBlanket,");
            strSql.Append("HighRiskEqui=@HighRiskEqui,");
            strSql.Append("EquiOperation=@EquiOperation,");
            strSql.Append("inWorkNum=@inWorkNum,");
            strSql.Append("unWorkNum=@unWorkNum,");
            strSql.Append("bedsnum=@bedsnum");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@DailyManagementID", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.VarChar,50),
					new SqlParameter("@MoonID", SqlDbType.Int,4),
					new SqlParameter("@DailyID", SqlDbType.NVarChar,-1),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@WoJu", SqlDbType.Int,4),
					new SqlParameter("@CanJu", SqlDbType.Int,4),
					new SqlParameter("@Rice", SqlDbType.Decimal,9),
					new SqlParameter("@Surfice", SqlDbType.Decimal,9),
					new SqlParameter("@Oil", SqlDbType.Decimal,9),
					new SqlParameter("@DishDate", SqlDbType.DateTime),
					new SqlParameter("@CheckDate", SqlDbType.DateTime),
					new SqlParameter("@IsCommunicate", SqlDbType.Bit,1),
					new SqlParameter("@Area", SqlDbType.Decimal,9),
					new SqlParameter("@Oilstove", SqlDbType.Decimal,9),
					new SqlParameter("@SmokeMachineWash", SqlDbType.Int,4),
					new SqlParameter("@FireExtinguisher", SqlDbType.Int,4),
					new SqlParameter("@FireBlanket", SqlDbType.Int,4),
					new SqlParameter("@HighRiskEqui", SqlDbType.Int,4),
					new SqlParameter("@EquiOperation", SqlDbType.NVarChar,-1),
					new SqlParameter("@inWorkNum", SqlDbType.Int,4),
					new SqlParameter("@unWorkNum", SqlDbType.Int,4),
                    new SqlParameter("@bedsnum", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.DailyManagementID;
            parameters[1].Value = model.GroupID;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.MoonID;
            parameters[4].Value = model.DailyID;
            parameters[5].Value = model.AddDate;
            parameters[6].Value = model.WoJu;
            parameters[7].Value = model.CanJu;
            parameters[8].Value = model.Rice;
            parameters[9].Value = model.Surfice;
            parameters[10].Value = model.Oil;
            parameters[11].Value = model.DishDate;
            parameters[12].Value = model.CheckDate;
            parameters[13].Value = model.IsCommunicate;
            parameters[14].Value = model.Area;
            parameters[15].Value = model.Oilstove;
            parameters[16].Value = model.SmokeMachineWash;
            parameters[17].Value = model.FireExtinguisher;
            parameters[18].Value = model.FireBlanket;
            parameters[19].Value = model.HighRiskEqui;
            parameters[20].Value = model.EquiOperation;
            parameters[21].Value = model.inWorkNum;
            parameters[22].Value = model.unWorkNum;
            parameters[23].Value = model.bedsnum;
            parameters[24].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DailyMoon ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DailyMoon ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.DailyMoon GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,DailyManagementID,GroupID,ClassID,MoonID,DailyID,AddDate,WoJu,CanJu,Rice,Surfice,Oil,DishDate,CheckDate,IsCommunicate,Area,Oilstove,SmokeMachineWash,FireExtinguisher,FireBlanket,HighRiskEqui,EquiOperation,inWorkNum,unWorkNum,bedsnum from DailyMoon ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            Catering_Model.DailyMoon model = new Catering_Model.DailyMoon();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.DailyMoon DataRowToModel(DataRow row)
        {
            Catering_Model.DailyMoon model = new Catering_Model.DailyMoon();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["DailyManagementID"] != null && row["DailyManagementID"].ToString() != "")
                {
                    model.DailyManagementID = int.Parse(row["DailyManagementID"].ToString());
                }
                if (row["GroupID"] != null && row["GroupID"].ToString() != "")
                {
                    model.GroupID = int.Parse(row["GroupID"].ToString());
                }
                if (row["ClassID"] != null)
                {
                    model.ClassID = row["ClassID"].ToString();
                }
                if (row["MoonID"] != null && row["MoonID"].ToString() != "")
                {
                    model.MoonID = int.Parse(row["MoonID"].ToString());
                }
                if (row["DailyID"] != null && row["DailyID"].ToString() != "")
                {
                    model.DailyID = row["DailyID"].ToString();
                }
                if (row["AddDate"] != null && row["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(row["AddDate"].ToString());
                }
                if (row["WoJu"] != null && row["WoJu"].ToString() != "")
                {
                    model.WoJu = int.Parse(row["WoJu"].ToString());
                }
                if (row["CanJu"] != null && row["CanJu"].ToString() != "")
                {
                    model.CanJu = int.Parse(row["CanJu"].ToString());
                }
                if (row["Rice"] != null && row["Rice"].ToString() != "")
                {
                    model.Rice = decimal.Parse(row["Rice"].ToString());
                }
                if (row["Surfice"] != null && row["Surfice"].ToString() != "")
                {
                    model.Surfice = decimal.Parse(row["Surfice"].ToString());
                }
                if (row["Oil"] != null && row["Oil"].ToString() != "")
                {
                    model.Oil = decimal.Parse(row["Oil"].ToString());
                }
                if (row["DishDate"] != null && row["DishDate"].ToString() != "")
                {
                    model.DishDate = DateTime.Parse(row["DishDate"].ToString());
                }
                if (row["CheckDate"] != null && row["CheckDate"].ToString() != "")
                {
                    model.CheckDate = DateTime.Parse(row["CheckDate"].ToString());
                }
                if (row["IsCommunicate"] != null && row["IsCommunicate"].ToString() != "")
                {
                    if ((row["IsCommunicate"].ToString() == "1") || (row["IsCommunicate"].ToString().ToLower() == "true"))
                    {
                        model.IsCommunicate = true;
                    }
                    else
                    {
                        model.IsCommunicate = false;
                    }
                }
                if (row["Area"] != null && row["Area"].ToString() != "")
                {
                    model.Area = decimal.Parse(row["Area"].ToString());
                }
                if (row["Oilstove"] != null && row["Oilstove"].ToString() != "")
                {
                    model.Oilstove = decimal.Parse(row["Oilstove"].ToString());
                }
                if (row["SmokeMachineWash"] != null && row["SmokeMachineWash"].ToString() != "")
                {
                    model.SmokeMachineWash = int.Parse(row["SmokeMachineWash"].ToString());
                }
                if (row["FireExtinguisher"] != null && row["FireExtinguisher"].ToString() != "")
                {
                    model.FireExtinguisher = int.Parse(row["FireExtinguisher"].ToString());
                }
                if (row["FireBlanket"] != null && row["FireBlanket"].ToString() != "")
                {
                    model.FireBlanket = int.Parse(row["FireBlanket"].ToString());
                }
                if (row["HighRiskEqui"] != null && row["HighRiskEqui"].ToString() != "")
                {
                    model.HighRiskEqui = int.Parse(row["HighRiskEqui"].ToString());
                }
                if (row["EquiOperation"] != null)
                {
                    model.EquiOperation = row["EquiOperation"].ToString();
                }
                if (row["inWorkNum"] != null && row["inWorkNum"].ToString() != "")
                {
                    model.inWorkNum = int.Parse(row["inWorkNum"].ToString());
                }
                if (row["unWorkNum"] != null && row["unWorkNum"].ToString() != "")
                {
                    model.unWorkNum = int.Parse(row["unWorkNum"].ToString());
                }
                if (row["bedsnum"] != null && row["bedsnum"].ToString() != "")
                {
                    model.bedsnum = int.Parse(row["bedsnum"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,DailyManagementID,GroupID,ClassID,MoonID,DailyID,AddDate,WoJu,CanJu,Rice,Surfice,Oil,DishDate,CheckDate,IsCommunicate,Area,Oilstove,SmokeMachineWash,FireExtinguisher,FireBlanket,HighRiskEqui,EquiOperation,inWorkNum,unWorkNum,bedsnum ");
            strSql.Append(" FROM DailyMoon ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,DailyManagementID,GroupID,ClassID,MoonID,DailyID,AddDate,WoJu,CanJu,Rice,Surfice,Oil,DishDate,CheckDate,IsCommunicate,Area,Oilstove,SmokeMachineWash,FireExtinguisher,FireBlanket,HighRiskEqui,EquiOperation,inWorkNum,unWorkNum,bedsnum ");
            strSql.Append(" FROM DailyMoon ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DailyMoon ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from DailyMoon T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "DailyMoon";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
